﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace SelfMonitor
{
    public partial class Wizard : Form
    {
        public string GClass { get; set; }
        public string ProjectName { get; set; }
        public string RootPath { get; set; }
        //public DateTime StartDate { get; set; }
        //public DateTime EndDate { get; set; }

        public delegate void ReturnValue(object a, ReturnEvent e);
        public ReturnValue RV;


        public Wizard(string GC, string RP)
        {
            InitializeComponent();
            RootPath = RP;
            LProjectClass.Text = GC;
            if (GC.Contains("Design"))
                GClass = "Design";
            else if (GC.Contains("Programming"))
                GClass = "Programming";
            else if (GC.Contains("Reciting"))
                GClass = "Reciting";
            else
                GClass = "NoClass";
        }

        private void Wizard_Load(object sender, EventArgs e)
        {
            textBox1.Text = "Example: Learn_PS";
            textBox1.ForeColor = Color.Gray;

            textBox2.Text = dateTimePicker1.Value.ToShortDateString();
            textBox3.Text = dateTimePicker2.Value.ToShortDateString();
            this.Focus();
        }

        private void textBox2_MouseEnter(object sender, EventArgs e)
        {

        }

        private void InitProjectXml(string XmlPath)
        {
            XmlDocument xmlDocument = new XmlDocument();

            // 声明 XML

            XmlDeclaration xmlDeclare = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);

            //创建根节点

            XmlElement elementRoot = xmlDocument.CreateElement("Project");
            //elementRoot.SetAttribute("Name", ProjectName);
            xmlDocument.AppendChild(elementRoot);

            XmlElement elementPInfo = xmlDocument.CreateElement("ProjectInfo");
            elementPInfo.SetAttribute("version", "1");
            elementRoot.AppendChild(elementPInfo);
            //************  Project Info **********************
            XmlElement elementPClass = xmlDocument.CreateElement("ProjectClass");
            elementPClass.InnerText = GClass;
            elementPInfo.AppendChild(elementPClass);

            XmlElement elementPName = xmlDocument.CreateElement("ProjectName");
            elementPName.InnerText = ProjectName;
            elementPInfo.AppendChild(elementPName);

            XmlElement elementPTaskNo = xmlDocument.CreateElement("TaskNumbers");
            elementPTaskNo.InnerText = "0";
            elementPInfo.AppendChild(elementPTaskNo);

            XmlElement elementPProgress = xmlDocument.CreateElement("Percent");
            elementPProgress.InnerText = "0";
            elementPInfo.AppendChild(elementPProgress);

            XmlElement elementPStart = xmlDocument.CreateElement("StartDate");
            elementPStart.SetAttribute("Type", "DateTime");
            elementPStart.InnerText = textBox2.Text;
            elementPInfo.AppendChild(elementPStart);

            XmlElement elementPEnd = xmlDocument.CreateElement("EndDate");
            elementPEnd.SetAttribute("Type", "DateTime");
            elementPEnd.InnerText = textBox3.Text;
            elementPInfo.AppendChild(elementPEnd);

            //************************************************
            //************* Main Tasks *************************
            XmlElement elementTask = xmlDocument.CreateElement("Task");
            elementTask.SetAttribute("ID", "1");
            elementTask.SetAttribute("Status", "Raw");
            elementRoot.AppendChild(elementTask);

            xmlDocument.Save(XmlPath);
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            

            if (ProjectName == null||(textBox1.Text.Contains(" ")))
            {
                textBox1.Focus();
                return;
            }

            if (dateTimePicker1.Value > dateTimePicker2.Value)
            {
                MessageBox.Show("Date Selection wrong!!!", "Selection Wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dateTimePicker1.Focus();
                return;

            }
            switch (GClass)
            {
                case "Design":
                    if (Directory.Exists(System.IO.Path.Combine(RootPath, ProjectName)))
                    {
                        MessageBox.Show("Project Exists,Select Another one!");
                        textBox1.Text = "";
                        textBox1.Focus();
                    }
                    else
                    {
                        Directory.CreateDirectory(System.IO.Path.Combine(RootPath, ProjectName));
                        string XmlPath = System.IO.Path.Combine(RootPath, ProjectName) + "\\" + ProjectName + ".xml";
                        InitProjectXml(XmlPath);
                    }
                    break;
                case "Programming":
                    if (Directory.Exists(System.IO.Path.Combine(RootPath, ProjectName)))
                    {
                        MessageBox.Show("Project Exists,Select Another one!");
                        textBox1.Text = "";
                        textBox1.Focus();
                    }
                    else
                    {
                        Directory.CreateDirectory(System.IO.Path.Combine(RootPath, ProjectName));
                        string XmlPath = System.IO.Path.Combine(RootPath, ProjectName) + "\\" + ProjectName + ".xml";
                        InitProjectXml(XmlPath);
                       
                    }
                    break;
                case "Reciting":
                    if (Directory.Exists(System.IO.Path.Combine(RootPath, ProjectName)))
                    {
                        MessageBox.Show("Project Exists,Select Another one!");
                        textBox1.Text = "";
                        textBox1.Focus();
                    }
                    else
                    {
                        Directory.CreateDirectory(System.IO.Path.Combine(RootPath, ProjectName));
                        string XmlPath = System.IO.Path.Combine(RootPath, ProjectName) + "\\" + ProjectName + ".xml";
                        InitProjectXml(XmlPath);     
                    }
                    break;
                default:
                    break;
            }
            ReturnEvent re = new ReturnEvent();
            re.PN = textBox1.Text;
            re.GC = GClass;
            throwEvent(re);
            this.Close();
        }

        protected void throwEvent(ReturnEvent e)
        {
            if (RV != null)
            {
                RV(this, e);
            }
        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ProjectName = textBox1.Text;
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.ForeColor = Color.Black;
            textBox1.Text = "";
        }

        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            if (textBox1.Text == null)
            {
                textBox1.Text = "Example: Learn_PS";
                textBox1.ForeColor = Color.Gray;
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            textBox2.Text = dateTimePicker1.Value.ToShortDateString();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            textBox3.Text = dateTimePicker2.Value.ToShortDateString();
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            dateTimePicker1.Value =DateTime.Parse(textBox2.Text);
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            dateTimePicker2.Value = Convert.ToDateTime(textBox3.Text);
        }

    }
    public class ReturnEvent : EventArgs
    {
        public string PN { get; set; }
        public string GC { get; set; }
    }
}
